package com.android.powerbankpad.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ws.universal.tools.base.BaseProgressDialog;

public abstract class BaseFragment extends Fragment {
    public Handler handler = new Handler();
    protected BaseProgressDialog mProgressDialog;
    private boolean isVisible, isCreated, isLoaded;
    private View rootView;

/*    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisible && isCreated && !isLoaded) {
            autoData();
            isLoaded = true;
        }
    }*/

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = getRootView(inflater, container);
            isCreated = true;
        }
        mProgressDialog = new BaseProgressDialog(getContext());
        initView();
       /* if (isVisible && isCreated) {
            autoData();
            isLoaded = true;
        }*/


        return rootView;
    }

    public abstract View getRootView(LayoutInflater inflater, ViewGroup container);

    public abstract void initView();

    public abstract void autoData();


    /***
     * 隐藏键盘 并清除焦点
     * @param mEtPassword
     */
    public void hideSoftInput(View mEtPassword) {
        mEtPassword.clearFocus();
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEtPassword.getWindowToken(), 0); //强制隐藏键盘
    }


    /***
     * 显示键盘
     * @param view
     */
    public void showSoftInput(View view) {
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(view, 0);
        }
    }


    public void showProgressDialog(Context activity, boolean cancelable, String msg) {
        showProgressDialog(activity, null, cancelable, msg);
    }


    public void showProgressDialog(Context activity, boolean cancelable) {
        showProgressDialog(activity, cancelable, "");
    }

    public void showProgressDialog(Context activity, BaseProgressDialog.OnCancelListener cancelListener, boolean cancelable, String msg) {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.setMessage(msg);

        if (cancelListener != null) {
            mProgressDialog.setOnCancelListener(cancelListener);
        }
        mProgressDialog.setCancelable(cancelable);
        mProgressDialog.show();
    }


    public void stopProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.stop();
        }

    }


    /**
     * 不带参数的跳转
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJump(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
    }

    /**
     * 带参数的跳转
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJump(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转然后退出当前页
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJumpKill(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
        getActivity().finish();
    }

    /**
     * 带参数跳转然后退出当前页
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJumpKill(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().finish();
    }
}
